package com.dycx.api.controller.manage.system;

import com.dycx.common.RestResponse;
import com.dycx.common.annotation.WriterLog;
import com.dycx.common.context.UserInfoContext;
import com.dycx.common.enums.BizTypeEnum;
import com.dycx.system.dto.AdminDTO;
import com.dycx.system.service.AdminService;
import com.dycx.system.service.AuthService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/manage/system/admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    @Resource
    private AuthService authService;

    /**
     * 添加
     **/
    @WriterLog(hideResponse = true, bizType = BizTypeEnum.SYS_ADMIN, bizValue = "{#adminDto.id}")
    @PostMapping("/insert")
    public RestResponse<?> insert(@RequestBody @Valid AdminDTO adminDto) {
        return RestResponse.success(adminService.insert(adminDto));
    }

    /**
     * 更新
     **/
    @WriterLog(bizType = BizTypeEnum.SYS_ADMIN, bizValue = "{#adminDto.id}")
    @PostMapping("/update")
    public RestResponse<?> update(@RequestBody @Valid AdminDTO adminDto) {
        return adminService.update(adminDto) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 更新状态
     **/
    @WriterLog(bizType = BizTypeEnum.SYS_ADMIN, bizValue = "{#adminDto.id}")
    @PostMapping("/updateStatus")
    public RestResponse<?> updateStatus(@RequestBody AdminDTO adminDto) {
        return adminService.updateStatus(adminDto) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 刪除
     **/
    @WriterLog(bizType = BizTypeEnum.SYS_ADMIN, bizValue = "{#adminId}")
    @GetMapping("/delete")
    public RestResponse<?> delete(int adminId) {
        return adminService.delete(adminId) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 重置密码
     **/
    @WriterLog(hideResponse = true, bizType = BizTypeEnum.SYS_ADMIN, bizValue = "{#adminId}")
    @GetMapping("/restPassword")
    public RestResponse<?> restPassword(int adminId) {
        return RestResponse.success(adminService.restPassword(adminId));
    }

    /**
     * 查询 根据主键 id 查询
     **/

    @GetMapping("/getInfoById")
    public RestResponse<?> getInfoById(int id) {
        return RestResponse.success(adminService.getInfoById(id));
    }

    /**
     * 获取所有用户
     **/
    @GetMapping("/getList")
    public RestResponse<?> getList(Integer status) {
        return RestResponse.success(adminService.getList(status));
    }

    /**
     * 查询 分页查询
     **/
    @PostMapping("/getPage")
    public RestResponse<?> getPage(@RequestBody AdminDTO dto) {
        return RestResponse.success(adminService.getPage(dto));
    }


    /**
     * 管理员登录
     **/
    @WriterLog(hideRequest = true, bizType = BizTypeEnum.SYS_ADMIN, bizValue = "{#adminDto.id}")
    @PostMapping("/login")
    public RestResponse<?> login(@RequestBody AdminDTO adminDto) {
        Map<String, String> map = new HashMap<>();
        map.put("token", authService.login(adminDto));
        return RestResponse.success(map);
    }

    @GetMapping("/getInfo")
    public RestResponse<?> getInfo() {
        return RestResponse.success(UserInfoContext.get());
    }

    @WriterLog(hideRequest = true, bizType = BizTypeEnum.SYS_ADMIN, bizValue = "{#adminDto.id}")
    @PostMapping("/updatePassword")
    public RestResponse<?> updatePassword(@RequestBody AdminDTO adminDto) {
        return adminService.updatePassword(adminDto) ? RestResponse.success() : RestResponse.fail();
    }
}
